//
//  VisitorView.swift
//  Weibo
//
//  Created by zxy on 2018/11/30.
//  Copyright © 2018 zxy. All rights reserved.
//

import UIKit

//swift定义代理必须继承NSObjectProtocol
protocol VistorDelegate : NSObjectProtocol {
    //登录
  func loginDidClick()
    //注册
  func registerDidClick()
}

class VisitorView: UIView {
    
    //声明一个代理对象
    weak var delegate: VistorDelegate?
    
   private lazy var iconImageView: UIImageView = {
        let lv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return lv
    }()
   private lazy var maskBgImageView: UIImageView = {
        let lv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return lv
    }()
   private lazy var houseImageView: UIImageView = {
        let lv = UIImageView()
        return lv
    }()
   private lazy var tipLab: UILabel = {
        let lab: UILabel = UILabel.cz_label(withText: "关注一些人，回这里看看有什么惊喜", fontSize: 16, color: UIColor(hexString: "#d0d0d0"))
        lab.textAlignment = .center
        return lab
    }()
    private lazy var registerBtn: UIButton = {
        let btn: UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: UIColor.orange, imageUrl: nil)
        btn.layer.cornerRadius = 3
        btn.clipsToBounds = true
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor(hexString: "#c0c0c0").cgColor
        btn.addTarget(self, action: #selector(registerClick(sender:)), for: .touchUpInside)
        return btn
    }()
   private lazy var loginBtn: UIButton = {
        let btn: UIButton = UIButton.cz_textButton("登录", fontSize: 16, normalColor: UIColor.darkGray, imageUrl: nil)
        btn.layer.cornerRadius = 3
        btn.clipsToBounds = true
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor(hexString: "#c0c0c0").cgColor
        btn.addTarget(self, action: #selector(loginClick(sender:)), for: .touchUpInside)
        return btn
    }()
    
    
    /// 设置各主页面的图片和提示文字
    ///
    /// - Parameters:
    ///   - isHome: 是否是首页
    ///   - houseImgName: 图片
    ///   - tipMessage: 提示文字
    func setupVisitorInfo(isHome: Bool,houseImgName:String,tipMessage:String){
        iconImageView.isHidden = !isHome
        houseImageView.image = UIImage(named: houseImgName)
        tipLab.text = tipMessage
        if isHome{
            startAniamtion()
        }
    }
    
    //执行动画
    private func startAniamtion(){
        let baseAni = CABasicAnimation(keyPath: "transform.rotation")
        baseAni.toValue = 2 * Double.pi
        baseAni.repeatCount = MAXFLOAT
        baseAni.duration = 10
        //结束时不移除动画，不然点击其它页面在点击首页，动画会停止
        baseAni.isRemovedOnCompletion = false
        iconImageView.layer.add(baseAni, forKey: nil)
    }
    
    //MARK:-注册
    @objc private func registerClick(sender:UIButton){
         delegate?.registerDidClick()
    }
    
    //MARK:-登录
    @objc private func loginClick(sender:UIButton){
        delegate?.loginDidClick()
    }
    
    override init(frame: CGRect) {
       super.init(frame: frame)
        backgroundColor = UIColor(hexString: "#EDEDED")
        addSubview(iconImageView)
        addSubview(maskBgImageView)
        addSubview(houseImageView)
        addSubview(tipLab)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //布局子控件
        iconImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-100)
        }
        
        maskBgImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView)
        }
        
        houseImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView).offset(60)
        }
        
        tipLab.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(50)
            make.right.equalTo(self).offset(-50)
            make.top.equalTo(houseImageView.snp_bottomMargin).offset(30)
            make.centerX.equalTo(houseImageView)
        }
        
        registerBtn.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(35)
            make.top.equalTo(tipLab.snp_bottomMargin).offset(30)
            make.right.equalTo(houseImageView.snp_centerXWithinMargins).offset(-10)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(35)
            make.left.equalTo(houseImageView.snp_centerXWithinMargins).offset(10)
            make.top.equalTo(registerBtn)
        }
    }
    
    
    
    //XIB、Stroyboard走这里，不写会崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
